{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.  定义RNN的参数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "X = [1,2]\n",
    "state = [0.0, 0.0]\n",
    "w_cell_state = np.asarray([[0.1, 0.2], [0.3, 0.4]])\n",
    "w_cell_input = np.asarray([0.5, 0.6])\n",
    "b_cell = np.asarray([0.1, -0.1])\n",
    "w_output = np.asarray([[1.0], [2.0]])\n",
    "b_output = 0.1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.  执行前向传播过程。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "before activation:  [ 0.6  0.5]\n",
      "state:  [ 0.53704957  0.46211716]\n",
      "output:  [ 1.56128388]\n",
      "before activation:  [ 1.2923401   1.39225678]\n",
      "state:  [ 0.85973818  0.88366641]\n",
      "output:  [ 2.72707101]\n"
     ]
    }
   ],
   "source": [
    "for i in range(len(X)):\n",
    "    before_activation = np.dot(state, w_cell_state) + X[i] * w_cell_input + b_cell\n",
    "    state = np.tanh(before_activation)\n",
    "    final_output = np.dot(state, w_output) + b_output\n",
    "    print \"before activation: \", before_activation\n",
    "    print \"state: \", state\n",
    "    print \"output: \", final_output"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
